<?php
// avoid excuted from web[http://].
if( isset($_SERVER['REQUEST_METHOD']) ) {
    echo "<html><body>Permission denied.</body></html>\n";
    exit();
}
require_once(dirname(__FILE__).'/../../inc/class/init.php');
WF::initCLI($argv, $argc);
/*
 * php.exe
 * interface_request.php $argv[0]
 * turnOnInterface.php $argv[1]
 * IDF $argv[2]
 * 1.0 $argv[3]
 * ...
 * 
 */

//create object WFExitCode and include_file /path/to/ExitCode.php
$objExitCode = WF::getConfigFactory()->getExitCode();
if ($argc < 2){
	$objExitCode->exitCli( WFExitCode::ERROR_WRONG_PARAMETER );
}else if(isset($argv [1]) && in_array ( $argv [1], array ('--help', '-help', '-h', '-?' ) )) {
	$objExitCode->exitCli( WFExitCode::SHOW_HELP );
} else {
	$strFileInterfaceInput = isset($argv[1]) ? $argv[1] : '';
	
	// below is a handler of parameter <filter_target>
	// If execute php /path/to/interface_request.php turnOnWidgetOfCategoryByVersion.php IDF 2.0
	// IDF represent <filter_target>
	// In WF20, only turnOnWidgetOfCategoryByVersion.php and turnOffWidgetOfCategoryByVersion.php need <filter_target>
	// Case 1: file_interface need <filter_target>
	//    If execute php /path/to/interface_request.php turnOnWidgetOfCategoryByVersion.php IDF 2.0
	//    <filter_target> = IDF = $argv[2]
	//    <params> = 2.0 = implode(' ', array_slice( $argv, 2 ))
	// Case 2: file_interface no need <filter_target>
	//    If execute php /path/to/interface_request.php synServerListType.php update
	//    <filter_target> = ''
	//    <params> = update = implode(' ', array_slice( $argv, 2 ))
	$arrFileInterfaceRequiredFilterTarget = WF::getConfigFactory()->getOpt()->getFileInterfaceRequiredFilterTarget();
	$strFilterTarget = '';
	if(in_array($strFileInterfaceInput,$arrFileInterfaceRequiredFilterTarget)){ //if file_interface require filter_target
	    $strFilterTarget = isset($argv[2]) ? $argv[2] : '';
	    $intArraySliceArgv = 3;
	}else{
	    $intArraySliceArgv = 2;
	}
	
	$arrFileInterface = WF::getConfigFactory()->getOpt()->getFileInterface();
	$strPathFileInterfaceInput = WF::getWidgetFactory()->getInterface()->getPathFileInterface($strFileInterfaceInput);
	if(false === in_array(basename($strFileInterfaceInput), $arrFileInterface)){
		if(!file_exists($strPathFileInterfaceInput)){
			$objExitCode->exitCli( WFExitCode::ERROR_INVALID_FILE_INTERFACE );
		}
	}
	//mydebug_log(implode('::'.$argv));
	$params = implode ( ' ', array_slice ( $argv, $intArraySliceArgv ) );
	$cmd = "";
	$cmd = "\"{$strPathFileInterfaceInput}\"";
	if(!empty($strFilterTarget)){
		$cmd .= " ".$strFilterTarget;
	}
	
    // Check is WF::getWidgetPoolFactory()->getWidgetPoolDB()->execDBUpdateFilter()
    $isThisCmdHaveToCheckSaveToDB = true;
    
	if(false !== strpos($params,'IS_execDBUpdateFilter')){ //if got execDBUpdateFilter
	    $isThisCmdHaveToCheckSaveToDB = false;
	    $params = rtrim(str_replace('IS_execDBUpdateFilter', '', $params));
	}
	
	if(!empty($params)){
		$cmd .= " ".$params;
	}
	
	
	// If Cmd is invalid
	$isCmdInterfaceValid = WF::getWidgetFactory()->getInterface()->isCmdInterfaceValid($strFileInterfaceInput, $strFilterTarget, $params);
	/*mydebug_log($cmd);
	mydebug_log($params);*/
	if(true !== $isCmdInterfaceValid){
	    $errorCode = $isCmdInterfaceValid;
	    $objExitCode->exitCli( $errorCode );
	}else{
	    if(true === WF::isUnitTest()){
		    $cmd .= " IS_UNITTEST";
		}
    	exec ( 'php -f '.$cmd , $arrOutput, $exitcode);
    	//mydebug_log('php -f '.$cmd);
    	//var_dump($cmd , $arrOutput, $exitcode);
    	if(0 !== $exitcode){
    	    $strMsg = 'Error CLI command! $cmd:'.$cmd.' | $arrOutput:'.WF::getVarLog($arrOutput).' | $exitcode:'.$exitcode;
    	    myerror_log($strMsg);
    	}
    	// Save filter to WP_UPDATE_FILTER
    	WF::getWidgetFactory()->getInterface()->saveFileInterfaceToDB($strFileInterfaceInput,$strFilterTarget,$params);
    	/*$arrFileInterfaceHaveToSaveToDB = WF::getConfigFactory()->getOpt()->getFileInterfaceHaveToSaveToDB();
    	if(false === WF::isExecDBUpdateFilter() && true === in_array($strFileInterfaceInput, $arrFileInterfaceHaveToSaveToDB)){
    		$arrData = array();
    		$arrData['file_interface'] = $strFileInterfaceInput;
    	    $arrData['filter_target'] = $strFilterTarget;
    	    $arrData['filter_condition'] = $params;
    		WF::getWidgetPoolFactory()->getWidgetPoolDB()->insertOrUpdateFilterByArrData($arrData);
    	}*/
	}
	$objExitCode->exitCli($exitcode, $strFileInterfaceInput);
}